package com.feidee.data.report.util;

import com.alibaba.fastjson.JSON;
import com.feidee.data.report.model.RunStatusModel;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class RunFlagController {
	// 存储当前报表
	private static ConcurrentHashMap<String, Map<Long, RunStatusModel>> runReportThreadCache =
			new ConcurrentHashMap<String, Map<Long, RunStatusModel>>();

	// CHQ 2021-04-07 报表重跑标识,key为report_key+"_"+report_index,重跑机制只针对跑失败的段、非失败段依旧hive on spark
	public static Map<String, String> reportIndexSQLIsRestartMap = new HashMap<>();

	// 检查是否有停止信号
	public static boolean checkRunFlag(String reportKey, long threadId) {
		Map<Long, RunStatusModel> runFlagMap = runReportThreadCache.get(reportKey);
		if ((runFlagMap != null) && (runFlagMap.size() != 0)) {
			if (runFlagMap.containsKey(threadId)) {
				return runFlagMap.get(threadId).getRunStatus();
			} else {
				return true;
			}
		}
		return true;
	}

	// 清除运行信息
	public static void clearRunFlag(String reportKey, long threadId) {
		Map<Long, RunStatusModel> runFlagMap = runReportThreadCache.get(reportKey);
		if ((runFlagMap != null) && (runFlagMap.size() != 0)) {
			runFlagMap.remove(threadId);
			if (0 == runFlagMap.size()) {
				runReportThreadCache.remove(reportKey);
			}
		}
	}

	// 添加运行信息
	public static void addRunFlag(String reportKey, long threadId) {
		Map<Long, RunStatusModel> runFlagMap = runReportThreadCache.get(reportKey);
		if ((runFlagMap != null) && (runFlagMap.size() != 0)) {
			Set<Long> threadIdSet = runFlagMap.keySet();
			for (Long runThreadId : threadIdSet) {
				if (! runThreadId.equals(threadId)) {
					RunStatusModel runStatusModel = runFlagMap.get(runThreadId);
					runStatusModel.setRunStatus(false);
					runFlagMap.put(runThreadId, runStatusModel);
				}
			}
		}
		if (runFlagMap == null) {
			runFlagMap = new ConcurrentHashMap<Long, RunStatusModel>();
			runReportThreadCache.put(reportKey, runFlagMap);
		}
		RunStatusModel runStatusModel = new RunStatusModel();
		runStatusModel.setRunStatus(true);
		runStatusModel.setStopByManual(false);
		runFlagMap.put(threadId, runStatusModel);
	}

	// 人为停止某个报表的执行
	public static void stopReportExecute(String reportKey) {
		Map<Long, RunStatusModel> runFlagMap = runReportThreadCache.get(reportKey);
		if ((runFlagMap != null) && (runFlagMap.size() != 0)) {
			Set<Long> threadIdSet = runFlagMap.keySet();
			for (Long runThreadId : threadIdSet) {
				RunStatusModel runStatusModel = runFlagMap.get(runThreadId);
				runStatusModel.setRunStatus(false);
				runStatusModel.setStopByManual(true);
				runFlagMap.put(runThreadId, runStatusModel);
			}
		}
	}

	// 检查是否是人为停止
	public static boolean checkStopRunFlag(String reportKey, long threadId) {
		Map<Long, RunStatusModel> runFlagMap = runReportThreadCache.get(reportKey);
		if ((runFlagMap != null) && (runFlagMap.size() != 0)) {
			if (runFlagMap.containsKey(threadId)) {
				return runFlagMap.get(threadId).getStopByManual();
			} else {
				return false;
			}
		}
		return false;
	}

	// 获取当前报表运行状态
	public static String getRunFlagCacheStr() {
		return JSON.toJSONString(runReportThreadCache);
	}

	public static ConcurrentHashMap<String, Map<Long, RunStatusModel>> getRunReportThreadCache() {
		return runReportThreadCache;
	}

	public static void setRunReportThreadCache(ConcurrentHashMap<String, Map<Long, RunStatusModel>> runReportThreadCache) {
		RunFlagController.runReportThreadCache = runReportThreadCache;
	}


}
